package com.controller;

import com.dto.AttendeeDTO;
import com.pojo.Attendee;
import com.pojo.Room;
import com.pojo.User;
import com.service.AttendeeService;
import com.service.UserService;
import com.utils.PageSupport;
import com.vo.AttendeeVO;
import com.vo.RoomVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

@Controller
public class AttendeeController {

    @Autowired
    private AttendeeService attendeeService;
    @Autowired
    private UserService userService;

    @RequestMapping("/meeting/attendeelist.html")
    public String getroomList(AttendeeVO attendeeVO, Model model)
    {
        System.out.println(attendeeVO);
        int pageSize=5;
        int tempPageIndex=1;
        if (attendeeVO.getPageIndex()!=null){
            tempPageIndex=attendeeVO.getPageIndex();
        }

        if(attendeeVO.getPageIndex()==null||attendeeVO.getPageIndex()<1) {
            attendeeVO.setPageIndex(1);
        }

        // 符合条件的总数量
        int totalCount=attendeeService.getAttendeeCount(attendeeVO.getMid(),
                                        attendeeVO.getReadStatus(),
                                        attendeeVO.getCheckStatus());

        System.out.println("totalCount="+totalCount);

        // 分页控制
        PageSupport pages=new PageSupport();
        pages.setCurrentPageNo(attendeeVO.getPageIndex());
        pages.setPageSize(pageSize);
        pages.setTotalCount(totalCount);
        int totalPageCount=pages.getTotalPageCount();
        System.out.println(totalPageCount);
        if(attendeeVO.getPageIndex()>totalPageCount) {
            attendeeVO.setPageIndex(totalPageCount);
            tempPageIndex = totalPageCount;
        }

        // 获取用户列表
        List<AttendeeDTO> attendeeDTOList = attendeeService.getAttendeeList(attendeeVO,pageSize);
        model.addAttribute("attendeeList",attendeeDTOList);


        model.addAttribute("mid",attendeeVO.getMid());
        model.addAttribute("readStatus",attendeeVO.getReadStatus());
        model.addAttribute("checkStatus",attendeeVO.getCheckStatus());
        model.addAttribute("totalPageCount",totalPageCount);
        model.addAttribute("totalCount",totalCount);
        System.out.println(attendeeVO.getPageIndex());
        model.addAttribute("pageIndex",tempPageIndex);


        return "/attendee/list";
    }

    @RequestMapping(value="/attendee/add.html",method= RequestMethod.GET)
    public String toAddroom(Attendee attendee, Integer mid, Model model) {
        List<User> userList = attendeeService.getStaffList();
        model.addAttribute("userList" , userList);
        model.addAttribute("mid",mid);
        return "/attendee/add";
    }

    @RequestMapping(value="/attendee/add.html",method=RequestMethod.POST)
    public String doAddRoom(@Valid Attendee attendee,
                            BindingResult bindingResult,
                            Model model) {

        if(bindingResult.hasErrors()) {

            return "redirect:/attendee/add.html?mid="+attendee.getMeetingid();
        }

        boolean val=attendeeService.insertAttendee(attendee);
        if(val) {
            // 成功：转跳用户列表页面
            return "redirect:/meeting/attendeelist.html?mid="+attendee.getMeetingid();
        }else{
            model.addAttribute("error","该用户已在会议室");
            // 失败：返回添加用户页面
            return "/attendee/add";
        }


    }


}
